﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

// This is the initial jQuery starting point.  The 'main' function is run once the DOM of every page is established.



// This function must be called with all modal dialog boxes which have fields that
// require validation.  It performs a call to the ASP.Net function, "Page_ClientValidate()",
// and then continues on calling "CloseModalDialog()" if the validation is okay.
// Note: Surprisingly, companion functions like "ValidatorValidate()" do not work, 
// perhaps due to the way ThickBox rearranges DOM elements.
function CheckValidationBeforeClose(element) {
    if (Page_ClientValidate())
        CloseModalDialog(element);
}


// Used to close a ThickBox modal dialog and then force a postback,
// which a server-side control is seemingly unable to do on its own
// when fired from within the confines of a ThickBox object.
function CloseModalDialog(element) {
    tb_remove();
    setTimeout('__doPostBack(\'' + element.name + '\',\'\')', 500);  // 500ms seems to give ThickBox enough time to remove itself
}


// Used to close a ThickBox modal dialog without causing a postback.
function CancelModalDialog() {
    tb_remove();
}



// Prepares all textboxes such that the background changes to a distinct
// color upon focus and then returns to white after focus is lost.
function PrepareDefaultEventHandlers() {
    $(":text").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $(":password").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $("textarea").focus(textboxHighlight).blur(textboxRemoveHighlight);
}

function textboxHighlight() {
    $("#" + this.id).css({ 'background-color': '#ffff40' });
}

function textboxRemoveHighlight() {
    $("#" + this.id).css({ 'background-color': 'white' });
}


var mangso = ['không', 'một', 'hai', 'ba', 'bốn', 'năm', 'sáu', 'bảy', 'tám', 'chín'];
function dochangchuc(so, daydu) {
    var chuoi = "";
    chuc = Math.floor(so / 10);
    donvi = so % 10;
    if (chuc > 1) {
        chuoi = " " + mangso[chuc] + " mươi";
        if (donvi == 1) {
            chuoi += " mốt";
        }
    } else if (chuc == 1) {
        chuoi = " mười";
        if (donvi == 1) {
            chuoi += " một";
        }
    } else if (daydu && donvi > 0) {
        chuoi = " lẻ";
    }
    if (donvi == 5 && chuc > 1) {
        chuoi += " lăm";
    } else if (donvi > 1 || (donvi == 1 && chuc == 0)) {
        chuoi += " " + mangso[donvi];
    }
    return chuoi;
}
function docblock(so, daydu) {
    var chuoi = "";
    tram = Math.floor(so / 100);
    so = so % 100;
    if (daydu || tram > 0) {
        chuoi = " " + mangso[tram] + " trăm";
        chuoi += dochangchuc(so, true);
    } else {
        chuoi = dochangchuc(so, false);
    }
    return chuoi;
}
function dochangtrieu(so, daydu) {
    var chuoi = "";
    trieu = Math.floor(so / 1000000);
    so = so % 1000000;
    if (trieu > 0) {
        chuoi = docblock(trieu, daydu) + " triệu";
        daydu = true;
    }
    nghin = Math.floor(so / 1000);
    so = so % 1000;
    if (nghin > 0) {
        chuoi += docblock(nghin, daydu) + " nghìn";
        daydu = true;
    }
    if (so > 0) {
        chuoi += docblock(so, daydu);
    }
    return chuoi;
}
function docso(so) {
    if (so == 0) return mangso[0];
    var chuoi = "", hauto = "";
    do {
        ty = so % 1000000000;
        so = Math.floor(so / 1000000000);
        if (so > 0) {
            chuoi = dochangtrieu(ty, true) + hauto + chuoi;
        } else {
            chuoi = dochangtrieu(ty, false) + hauto + chuoi;
        }
        hauto = " tỷ";
    } while (so > 0);
    return chuoi;
}

function isNumberKeyCal(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 45 || charCode == 42 || charCode == 43 || charCode == 47) return true;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        evt.cancel = true;
        evt.returnValue = false;
        return false;
    }
    return true;
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        evt.cancel = true;
        evt.returnValue = false;
        return false;
    }
    return true;
}
// thousand separator /  add commas (VD: 1,000)
function addCommas(obj) {
    var nStr = $("#" + obj).val();
    nStr = nStr.replace(/,/g, "")
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    $("#" + obj).val(x1 + x2);
}
function addCommasValue(nStr) {
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function numberWithCommas(x) {
    return x.toString().replace(/,/g, '').replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// kiem tra số từ và đến (số từ phải nhỏ hơn hoặc bằng với số đến)
function CompareFromTo(obj1, obj2) {
    var valueOne = $("#" + obj1).val();
    var valueTwo = $("#" + obj2).val();
    if (valueOne.trim().length > 0 && valueTwo.trim().length > 0) {
        valueOne = parseFloat(valueOne.replace(",", ""));
        valueTwo = parseFloat(valueTwo.replace(",", ""));
        if (valueOne <= valueTwo) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return true;
    }
}

function OpenInNewTab(url) {
    setTimeout(function () {
        var win = window.open(url, '_blank');
        win.focus();
    }
    , 2000)
}

